package com.happybluefin.tapburstfree.common.scoreloop;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.scoreloop.client.android.core.controller.RankingController;
import com.scoreloop.client.android.core.controller.RequestController;
import com.scoreloop.client.android.core.controller.RequestControllerObserver;
import com.scoreloop.client.android.core.controller.ScoreController;
import com.scoreloop.client.android.core.controller.ScoresController;
import com.scoreloop.client.android.core.model.Score;
import com.scoreloop.client.android.core.model.ScoreFormatter;

public class Scoreloop {
    public Scoreloop(ScoreloopCallback callback) {
        Log.d(TAG, "Scoreloop() start");

        mCallback = callback;

        Log.d(TAG, "Scoreloop() end");
    }

    public void submitScore(String name, double value) {
        Log.d(TAG, "submitScore() start");

        Score score = new Score(value, null);
        RequestControllerObserver observer = new RequestControllerObserver() {
            @Override
            public void requestControllerDidFail(RequestController controller, Exception exception) {
                //提交失败
                if (mCallback != null) {
                    mCallback.submitResult(false);
                }
            }

            @Override
            public void requestControllerDidReceiveResponse(RequestController controller) {
                //提交成功
                if (mCallback != null) {
                    mCallback.submitResult(true);
                }
            }
        };
        mScoreController = new ScoreController(observer);
        mScoreController.submitScore(score);

        Log.d(TAG, "submitScore() end");
    }

    public void getLeaderboard(int size) {
        Log.d(TAG, "getLeaderboard() start");

        final RequestControllerObserver observer = new RequestControllerObserver() {
            @Override
            public void requestControllerDidFail(final RequestController aRequestController, final Exception anException) {
                //取得失败
                if (mCallback != null) {
                    mCallback.getLeaderboardResult(false, null);
                }
            }

            @Override
            public void requestControllerDidReceiveResponse(final RequestController requestController) {
                //取得成功
                final ScoresController scoresController = (ScoresController) requestController;
                final List<Score> scores = scoresController.getScores();

                final List<String> scoreList = new ArrayList<String>();
                int i = 0;
                for (final Score score : scores) {
                    scoreList.add(++i + ". " + score.getUser().getDisplayName() + " - " + ScoreFormatter.format(score));
                }

                if (mCallback != null) {
                    mCallback.getLeaderboardResult(true, scoreList);
                }
            }
        };
        mScoresController = new ScoresController(observer);
        mScoresController.setRangeLength(size);
        mScoresController.loadRangeAtRank(1);

        Log.d(TAG, "getLeaderboard() end");
    }

    public void getRank(double value) {
        Log.d(TAG, "getRank() start");

        RequestControllerObserver observer = new RequestControllerObserver() {
            @Override
            public void requestControllerDidFail(RequestController aRequestController, Exception anException) {
                if (mCallback != null) {
                    mCallback.getRankResult(false, -1, -1);
                }
            }

            @Override
            public void requestControllerDidReceiveResponse(RequestController aRequestController) {
                Integer rank = mRankingController.getRanking().getRank();
                Integer total = mRankingController.getRanking().getTotal();
                if (mCallback != null) {
                    mCallback.getRankResult(false, rank.intValue(), total.intValue());
                }
            }
        };
        Score score = new Score(value, null);

        mRankingController = new RankingController(observer);
        mRankingController.loadRankingForScore(score);

        Log.d(TAG, "getRank() end");
    }

    protected ScoreloopCallback mCallback = null;

    protected ScoreController mScoreController = null;

    protected ScoresController mScoresController = null;

    protected RankingController mRankingController = null;

    protected static final String TAG = "TapBurstFreeApplication";
}